﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OCR.Function
{
    class MultiTextDrawer
    {
        public Bitmap DrawTexts(Bitmap baseImage, List<TextPosition> textItems)
        {
            using (Graphics g = Graphics.FromImage(baseImage))
            {
                // 高质量渲染设置
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

                foreach (var item in textItems)
                {
                    // 测量文本实际占用空间
                    SizeF textSize = g.MeasureString(item.Text, item.Font);

                    // 计算最终绘制位置（支持百分比坐标）
                    PointF drawPos = item.IsPercentagePosition ?
                        new PointF(baseImage.Width * item.Position.X / 100f,
                                  baseImage.Height * item.Position.Y / 100f) :
                        item.Position;

                    // 绘制文本背景（可选）
                    if (item.BackColor.HasValue)
                    {
                        RectangleF bgRect = new RectangleF(
                            drawPos.X - 2,
                            drawPos.Y - 2,
                            textSize.Width + 4,
                            textSize.Height + 4);
                        using (Brush bgBrush = new SolidBrush(item.BackColor.Value))
                            g.FillRectangle(bgBrush, bgRect);
                    }

                    // 绘制文本主体
                    g.DrawString(item.Text, item.Font, item.Brush, drawPos);
                }
            }
            return baseImage;
        }
    }
    public class TextPosition
    {
        public string Text { get; set; }
        public PointF Position { get; set; }
        public bool IsPercentagePosition { get; set; }
        public Font Font { get; set; }
        public Brush Brush { get; set; }
        public Color? BackColor { get; set; }

        // 绝对坐标构造函数
        public TextPosition(string text, PointF position, Font font, Color color)
        {
            Text = text;
            Position = position;
            Font = font;
            Brush = new SolidBrush(color);
        }

        // 百分比坐标构造函数
        public TextPosition(string text, float xPercent, float yPercent, Font font, Color color)
        {
            Text = text;
            Position = new PointF(xPercent, yPercent);
            IsPercentagePosition = true;
            Font = font;
            Brush = new SolidBrush(color);
        }
    }
}

